package kyz.icom.customadapter;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private ListView listView1;
	public Weather[] weather_data;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		weather_data = new Weather[] {
				new Weather(R.drawable.weather_snow, "Cloudy"),
				new Weather(R.drawable.weather_showers, "Showers"),
				new Weather(R.drawable.weather_snow, "Snow"),
				new Weather(R.drawable.weather_showers, "Storm"),
				new Weather(R.drawable.weather_snow, "Sunny") };

		WeatherAdapter adapter = new WeatherAdapter(this,
				R.layout.listview_item_row, weather_data);

		listView1 = (ListView) findViewById(R.id.listView1);

		View header = (View) getLayoutInflater().inflate(
				R.layout.listview_header_row, null);
		listView1.addHeaderView(header);

		listView1.setAdapter(adapter);
		registerForContextMenu(listView1);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if (v.getId() == R.id.listView1) {
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

			// menu.setHeaderTitle(weather_data[info.position]);

			menu.setHeaderTitle("menu");
			String[] menuItems = getResources().getStringArray(
					R.array.menu_main);
			for (int i = 0; i < menuItems.length; i++) {
				menu.add(Menu.NONE, i, i, menuItems[i]);
			}
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		int menuItemIndex = item.getItemId();
		String[] menuItems = getResources().getStringArray(R.array.menu_main);
		String menuItemName = menuItems[menuItemIndex];
		//String listItemName = Weather[info.position];

		TextView text = (TextView) findViewById(R.id.txtTitle);
		//text.setText(String.format("Selected %s for item %s", menuItemName,
		//		listItemName));
		return true;
	}

}
